package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;

/**
 * Created by qingshengzheng on 15/8/31.
 */
public abstract class BaseListHeaderAdapter<VH extends BaseRecycleViewHolder,VC,VD>  extends HeaderFooterStatusRecyclerViewAdapter<VH> {
    private Context context;
    public  List<VC> datas=new ArrayList<VC>();
    public List<VD> headerDatas=new ArrayList<VD>();
    private LayoutInflater layoutInflater;


    public int getSpanSize(int position) {
        int hc = getHeaderItemCount();
        int cc = getContentItemCount();
        if (position < hc) {
            return 2;
        } else if (position < cc + hc) {
            return 1;
        } else {
            return 2;
        }
    }
    public int getSpanIndex(int position, int spanCount) {
        int hc = getHeaderItemCount();
        int cc = getContentItemCount();
        if (position < hc) {
            return 0;
        } else if (position >= hc && position < cc + hc) {
            return (position - hc) % spanCount;
        } else {
            return 0;
        }
    }

    public BaseListHeaderAdapter(Context context)
    {
        this.context=context;
        this.layoutInflater=LayoutInflater.from(context);
    }

    public void initDatas(List<VC> datas) {
        this.datas = datas;
        notifyContentItemRangeInserted(0,datas.size());
    }

    public void clearDatasWithoutChanged()
    {
        this.datas.clear();
    }
    public void clearDatas()
    {
        int count=this.datas.size();
        this.datas.clear();
        notifyContentItemRangeRemoved(0,count);
    }
    public void addDatas(List<VC> datas)
    {
        int start=this.datas.size();
        int limit=datas.size();
        this.datas.addAll(datas);
        notifyItemRangeInserted(start,limit);

    }
    public void addData(VC data)
    {
        datas.add(data);
        notifyContentItemInserted(datas.size()-1);
    }

    public abstract  VH createViewHolder(ViewGroup parent);

    public  abstract   VH createHeaderHolder(ViewGroup parent);





    @Override
    public  int getHeaderItemCount() {
        return headerDatas.size();
    }

    @Override
    public  int getContentItemCount() {
        return datas.size();
    }

    @Override
    protected VH onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        return createHeaderHolder(parent);
    }

    @Override
    protected VH onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        return createViewHolder(parent);
    }

    @Override
    protected void onBindHeaderItemViewHolder(VH headerViewHolder, int position) {
        headerViewHolder.renderView(headerDatas.get(position),position);

    }

    @Override
    protected void onBindContentItemViewHolder(VH contentViewHolder, int position) {
        contentViewHolder.renderView(datas.get(position),position);

    }

    public static  abstract class BaseListViewHolder<VC> extends  RecyclerView.ViewHolder{
        public Context context;
        public LayoutInflater layoutInflater;


        public BaseListViewHolder(View itemView) {
            super(itemView);
            this.context=itemView.getContext();
            this.layoutInflater=LayoutInflater.from(context);
        }
        public  abstract void  renderView(VC data,int position );

    }

}
